package com.qimou.iems.service.impl;

import com.qimou.iems.entity.PageArgs;
import com.qimou.iems.entity.PageList;
import com.qimou.iems.entity.User;
import com.qimou.iems.entity.UserRole;
import com.qimou.iems.exception.InterfaceCommonException;
import com.qimou.iems.mapper.IUserMapper;
import com.qimou.iems.mapper.IUserRoleMapper;
import com.qimou.iems.service.IUserService;
import com.qimou.iems.util.StatusConstant;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class UserServiceImpl implements IUserService {

    @Resource
    private IUserMapper userMapper;


    @Override
    public User getUserByPhone(String phone) {
        return userMapper.queryUserByPhone(phone);
    }


    @Override
    public void update(User user) throws Exception {
        if(null == user.getId()){
            throw new InterfaceCommonException(StatusConstant.FIELD_NOT_NULL,"参数不能为空");
        }
        userMapper.update(user);
    }


    @Override
    public PageList<User> getUserList(Map<String, Object> map, PageArgs pageArgs) {
        PageList<User> page = new PageList<User>();
        int count = userMapper.countUser(map);
        if(count > 0){
            map.put("pageArgs",pageArgs);
            page.setList(userMapper.queryUserList(map));
        }
        page.setTotalSize(count);
        return page;
    }


    @Transactional
    @Override
    public void save(User user) {
        userMapper.insert(user);
    }
}
